//
//  ADRadioButton.m
//  ADKit
//
//  Created by duzexu on 2018/1/4.
//

#import "ADRadioButton.h"

static NSMutableDictionary *_groupRadioDic = nil;

@interface ADRadioButton ()

@property(nonatomic, copy)  NSString *groupId;

@end

@implementation ADRadioButton

+ (instancetype)buttonWithSpace:(NSInteger)buttonSpace delegate:(id<ADRadioButtonDelegate>)delegate groupId:(NSString *)groupId {
    return [self buttonWithLayout:ADButtonLayoutNormal space:buttonSpace delegate:delegate groupId:groupId];
}

+ (instancetype)buttonWithLayout:(ADButtonLayout)buttonLayout space:(NSInteger)buttonSpace delegate:(id<ADRadioButtonDelegate>)delegate groupId:(NSString *)groupId {
    ADRadioButton *btn = [ADRadioButton buttonWithType:UIButtonTypeCustom];
    btn.layout = buttonLayout;
    btn.space = buttonSpace;
    btn.delegate = delegate;
    btn.groupId = groupId;
    return btn;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addTarget:self action:@selector(radioBtnChecked) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addToGroup];
        [self addTarget:self action:@selector(radioBtnChecked) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)dealloc {
    [self removeFromGroup];
    _delegate = nil;
    _groupId = nil;
}

- (void)setGroupId:(NSString *)groupId {
    _groupId = [groupId copy];
    [self addToGroup];
}

- (void)addToGroup {
    if(!_groupRadioDic) {
        _groupRadioDic = [[NSMutableDictionary alloc] init];
    }
    if (_groupId) {
        NSHashTable *_gRadios = [_groupRadioDic objectForKey:_groupId];
        if (!_gRadios) {
            _gRadios = [NSHashTable weakObjectsHashTable];
        }
        [_gRadios addObject:self];
        [_groupRadioDic setObject:_gRadios forKey:_groupId];
    }
}

- (void)removeFromGroup {
    if (_groupRadioDic) {
        NSHashTable *_gRadios = [_groupRadioDic objectForKey:_groupId];
        if (_gRadios) {
            [_gRadios removeObject:self];
            if (_gRadios.count == 0) {
                [_groupRadioDic removeObjectForKey:_groupId];
            }
        }
    }
}

- (void)radioBtnChecked {
    if (_checked) {
        if (_unCheckable) {
            [self changeCheckState:NO];
        }
        return;
    }
    [self changeCheckState:YES];
}

- (void)uncheckOtherRadios {
    NSHashTable *_gRadios = [_groupRadioDic objectForKey:_groupId];
    if (_gRadios.count > 0) {
        for (ADRadioButton *_radio in _gRadios) {
            if (_radio.checked && ![_radio isEqual:self]) {
                _radio.checked = NO;
            }
        }
    }
}

// 当前状态改变
- (void)changeCheckState:(BOOL)state {
    self.checked = state;
    
    if ([_delegate respondsToSelector:@selector(didSelectedRadioButton:groupId:)]) {
        [_delegate didSelectedRadioButton:self groupId:_groupId];
    }
}

#pragma mark -- setter
- (void)setChecked:(BOOL)checked{
    if (_checked == checked) {
        return;
    }
    _checked = checked;
    self.selected = checked;
    if (self.selected) {
        [self uncheckOtherRadios];
    }
}

@end
